<?php

namespace app\admin\controller;

use app\common\controller\Backend;

class Post extends Backend
{
    public $navTitle = 'post';

    public function __construct()
    {
        parent::__construct();

        $this->PostModel = model('Post.Post');
        $this->CateModel = model('Post.Cate');
    }

    public function index()
    {
        // TODO 
        $postList = $this->PostModel->with(['user', 'cate'])->paginate(10);

        if ($this->request->isGet()) {
            $search_field = $this->request->param('search_field', '');
            $keyword = $this->request->param('keyword', '');

            if ($keyword == '') $postList = $this->PostModel->with(['user', 'cate'])->paginate(10);

            if ($search_field == 'title') {
                $postList = $this->PostModel
                    ->with(['user', 'cate'])
                    ->where('title', 'like', "%{$keyword}%")
                    ->paginate(10);
            }

            if ($search_field == 'userid') {
                $user = model('User.User')->where('nickname', $keyword)->find();

                $postList = $this->PostModel
                    ->with(['user', 'cate'])
                    ->where('userid', 'like', "%{$user['id']}%")
                    ->paginate(10);
            }
        }

        return $this->fetch('', [
            'navTitle' => $this->navTitle,
            'postList' => $postList
        ]);
    }

    public function cate()
    {
        // TODO 
        $cateList = model('Post.Cate')->paginate(10);

        foreach ($cateList as $item) {
            $postList = model('Post.Post')->where('cateid', $item['id'])->select();

            $item['count'] = $postList ? count($postList) : 0;
        }

        return $this->fetch('', [
            'navTitle' => $this->navTitle,
            'cateList' => $cateList,
        ]);
        // 拿id
        // $result = $this->CommentModel->onlyTrashed()->whereIn('id',$idArr)->update(['deletetime' => null]);

        // UPDATE `pre_admin` INTO `deletetime` = null WHERE `id` IN 1,2,3
    }

    public function editcate()
    {
        $cateid = $this->request->param('cateid', '');

        $cate = model('Post.Cate')->find($cateid);

        if (!$cate) $this->error('分类不存在');

        if ($this->request->isPost()) {
            // TODO 接收参数
            $formData = $this->request->param();

            // TODO 验证码
            // if(!captcha_check($formData['vercode'])) $this->error('验证码错误');

            // halt($formData);
            $cate = $this->CateModel->find($formData['id']);

            if (!$cate) $this->error('分类不存在');

            // TODO 封装参数
            $data = [
                'id' => $formData['id'],
                'name' => $formData['name'],
                'weigh' => $formData['weigh'],
            ];

            $result = $this->CateModel->isUpdate()->save($data);

            if ($result === FALSE) $this->error('修改失败，' . $this->CateModel->getError());

            $this->success('修改成功', url('admin/post/cate'));
        }

        return $this->fetch('', [
            'navTitle' => $this->navTitle,
            'cate' => $cate,
        ]);
    }

    public function info()
    {
        $postid = $this->request->param('postid', '');

        $post = $this->PostModel->with(['user', 'cate'])->where('post.id', $postid)->find();

        if (!$post) {
            $this->error('帖子不存在');
        }

        return $this->fetch('', [
            'navTitle' => $this->navTitle,
            'post' => $post,
        ]);
    }

    public function delete()
    {
        if ($this->request->isAjax()) {
            $action = $this->request->param('action', '');

            if ($action == 'del') {
                $postid = $this->request->param('postid', '');

                $post = $this->PostModel->find($postid);

                if (!$post) $this->error('帖子不存在');

                $result = $this->PostModel->destroy($postid);

                $result === FALSE ? $this->error('删除失败') : $this->success('删除成功', url('admin/user/index'));
            }

            if ($action == 'dels') {
                // TODO 接收所有数据
                $formData = $this->request->param();
                // TODO 转存idArr
                $idArr = $formData['idArr'];

                $result = $this->PostModel->destroy($idArr);

                $result === FALSE ? $this->error('删除失败') : $this->success('删除成功');
            }
        }
    }

    public function appman()
    {
        $postList = $this->PostModel->onlyTrashed()->paginate(10);

        // halt($postList);
        $formData = $this->request->param();

        if ($this->request->isAjax()) {

            $action = $this->request->param('action');

            if ($action == 'return') {
                $postid = $this->request->param('postid', '');

                $post = $this->PostModel->onlyTrashed()->find($postid);

                if (!$post) $this->error('帖子不存在');

                $post->restore() === FALSE ? $this->error('还原失败') : $this->success('还原成功', url('admin/post/index'));
            }

            if ($action == 'returns') {
                // TODO 转存idArr
                $idArr = $formData['idArr'] ? $formData['idArr'] : [];

                $result = $this->PostModel->onlyTrashed()->whereIn('id', $idArr)->update(['deletetime' => null]);

                $result === FALSE ? $this->error(' 还原失败,') : $this->success('还原成功');
            }

            if ($action == 'del') {
                $postid = $this->request->param('postid', '');
                // halt($postid);
                $post = $this->PostModel->onlyTrashed()->find($postid);
                // halt($post);

                if (!$post) $this->error('帖子不存在');

                $result = $this->PostModel->destroy($postid, true);

                $result === FALSE ? $this->error('删除失败') : $this->success('删除成功');
            }

            if ($action == 'dels') {
                // TODO 接收所有数据
                $formData = $this->request->param();
                // TODO 转存idArr
                $idArr = $formData['idArr'];

                $result = $this->PostModel->destroy($idArr, true);

                $result === FALSE ? $this->error('删除失败') : $this->success('删除成功');
            }
        }

        return $this->fetch('', [
            'navTitle' => $this->navTitle,
            'postList' => $postList,
        ]);
    }
}
